package com.demonlover.oauth2.resource.controller;

import com.demonlover.oauth2.resource.domain.Content;
import com.demonlover.oauth2.resource.service.ContentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.annotation.Secured;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.security.PermitAll;
import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * @author WuChao
 * @version 1.0
 * @date 2020/6/3 12:04
 */
@RestController
@Secured({"ROLE_PORTRAIT"})

public class ContentController {

    @Autowired
    private ContentService contentService;

    @GetMapping("/")
    public List<Content> getContentList() {
        String name = SecurityContextHolder.getContext().getAuthentication().getName();
        System.out.println(name);
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        return contentService.list();


    }

    @PostMapping("/save/image")
    @PermitAll
    public void savaImage(MultipartFile file) {
        File uploadFile = new File("D://a" + "/" + file.getOriginalFilename());
        if (!uploadFile.exists()) {
            uploadFile.mkdirs();
        }
        try {
            file.transferTo(uploadFile);
        } catch (IOException e) {
            e.printStackTrace();
        }


    }
}
